package modules.march
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import ui.TextureSelectPanelUI;
	import util.Textures;
	import util.Tools;
	
	/**
	 * ...
	 * @author WLDragon 2013-12-22
	 */
	public class TextureSelectPanel extends TextureSelectPanelUI
	{
		
		public function TextureSelectPanel()
		{
			super();
			init();
		}
		
		private function init():void
		{
			mContainer = new Sprite;
			mContainer.x = 6;
			mContainer.y = 6;
			addChild(mContainer);
			
			this.buttonMode = true;
			this.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN,onDown);
			this.addEventListener(MouseEvent.RIGHT_MOUSE_UP,onUp);
			mContainer.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onUp(e:MouseEvent):void 
		{
			stopDrag();
		}
		
		private function onDown(e:MouseEvent):void 
		{
			startDrag();
		}
		
		private function onClick(e:MouseEvent):void
		{
			var t:TextureSelectItem = e.target as TextureSelectItem;
			if (t != null)
			{
				mCall.call(null,t.index);
			}
		}
		
		/**
		 * 设置纹理
		 * @param	$call 回调函数，点击纹理时执行
		 */
		public function setTextures($call:Function):void
		{
			mCall = $call;
			var w:int = Tools.textures.w;
			var h:int = Tools.textures.h;
			for (var i:int = 0; i < Textures.ROW; i++)
			{
				for (var j:int = 0; j < Textures.COL; j++)
				{
					var ts:TextureSelectItem = new TextureSelectItem;
					var index:int = (i + 1) * 10 + (j + 1);
					var d:BitmapData = Tools.textures.getData(index);
					if (d != null)
					{
						ts.imgTexture.bitmapData = d;
						ts.index = index
					}
					ts.x = w * j;
					ts.y = h * i;
					mContainer.addChild(ts);
				}
			}
		}
		
		private var mContainer:Sprite;
		private var mCall:Function;
	}

}